log using Table1_Figures.smcl,replace
use  NER_panel_data.dta, clear
gen country=1
append using NIGERIA_panel_data.dta
replace country=2 if country ==.

append using ETHIOPIA_panel_data.dta
replace country=3 if country ==.

append using UGA_panel_data.dta
replace country=4 if country ==.

append using TZA_panel_data.dta
replace country=5 if country ==.

append using MWI_panel_data.dta
replace country=6 if country ==.

xtset HHID_panel year

label define country 1 "Niger" 2 "Nigeria" 3 "Ethiopia" 4 "Uganda" 5 "Tanzania" 6 "Malawi"
label values country country
********************************************************************************
*                               Table 1                                        *
********************************************************************************
preserve
mat Y = J(100,5,.)
local tt "  hdd9 hdd9_own hdd9_purchase hdd9_gift  own_value_share purchase_value_share other_value_share"
foreach x of varlist    own_value_share purchase_value_share other_value_share other_crop{
replace `x'=`x'*100
}

local g 1
foreach var of varlist `tt'  {
ttest `var'=0
mat Y[`g',1] = r(mu_1)
local g=`g'+1
mat Y[`g',1] = r(sd_1)
local g=`g'+1

}

matrix rownames Y = hdd9 sd hdd9_own sd hdd9_purchase sd hdd9_gift sd own_value_share sd purchase_value_share sd other_value_share sd

matrix colnames Y =  % sd

matrix list Y
frmttable using Table_1_Whole.doc, replace  statmat(Y) ctitle("", "mean", "sd") sdec(2)
restore





preserve
keep if country==3|country==6
mat Y = J(100,5,.)
local tt "  hdd9 hdd9_own hdd9_purchase hdd9_gift  own_value_share purchase_value_share other_value_share"
foreach x of varlist    own_value_share purchase_value_share other_value_share other_crop{
replace `x'=`x'*100
}


local g 1
foreach var of varlist `tt'  {
ttest `var', by(country)
mat Y[`g',1] = r(mu_1)
mat Y[`g',2] = r(mu_2)
local g=`g'+1
mat Y[`g',1] = r(sd_1)
mat Y[`g',2] = r(sd_2)
local g=`g'+1

}

matrix rownames Y = hdd9 sd hdd9_own sd hdd9_purchase sd hdd9_gift sd own_value_share sd purchase_value_share sd other_value_share sd

matrix colnames Y =  % sd

matrix list Y
frmttable using Table_1_Ethiopia_Malawi.doc, replace  statmat(Y) ctitle("", "Ethiopia", "Malawi") sdec(2)
restore




preserve
keep if country==1|country==2
mat Y = J(100,5,.)
local tt "  hdd9 hdd9_own hdd9_purchase hdd9_gift  own_value_share purchase_value_share other_value_share"
foreach x of varlist    own_value_share purchase_value_share other_value_share other_crop{
replace `x'=`x'*100
}


local g 1
foreach var of varlist `tt'  {
ttest `var', by(country)
mat Y[`g',1] = r(mu_1)
mat Y[`g',2] = r(mu_2)
local g=`g'+1
mat Y[`g',1] = r(sd_1)
mat Y[`g',2] = r(sd_2)
local g=`g'+1

}

matrix rownames Y = hdd9 sd hdd9_own sd hdd9_purchase sd hdd9_gift sd own_value_share sd purchase_value_share sd other_value_share sd

matrix colnames Y =  % sd

matrix list Y
frmttable using Table_1_Niger_Nigeria.doc, replace  statmat(Y) ctitle("", "Niger", "Nigeria") sdec(2)
restore



preserve
keep if country==4|country==5
replace country=0 if country==5     //change the order of country

mat Y = J(100,5,.)
local tt "  hdd9 hdd9_own hdd9_purchase hdd9_gift  own_value_share purchase_value_share other_value_share"
foreach x of varlist    own_value_share purchase_value_share other_value_share other_crop{
replace `x'=`x'*100
}


local g 1
foreach var of varlist `tt'  {
ttest `var', by(country)
mat Y[`g',1] = r(mu_1)
mat Y[`g',2] = r(mu_2)
local g=`g'+1
mat Y[`g',1] = r(sd_1)
mat Y[`g',2] = r(sd_2)
local g=`g'+1

}

matrix rownames Y = hdd9 sd hdd9_own sd hdd9_purchase sd hdd9_gift sd own_value_share sd purchase_value_share sd other_value_share sd

matrix colnames Y =  % sd

matrix list Y
frmttable using Table_1_Tanzania_Uganda.doc, replace  statmat(Y) ctitle("", "Tanzania", "Uganda") sdec(2)
restore
********************************************************************************
*                               Table 1                                        *
********************************************************************************



preserve
drop if non_farmer==1

mat Y = J(100,5,.)
local tt "   no_species pdd9 pdd_livestock pdd_crop other_crop"
foreach x of varlist     other_crop{
replace `x'=`x'*100
}

local g 1
foreach var of varlist `tt'  {
ttest `var'=0
mat Y[`g',1] = r(mu_1)
local g=`g'+1
mat Y[`g',1] = r(sd_1)
local g=`g'+1

}

matrix rownames Y =  no_species sd pdd9 sd pdd_livestock sd pdd_crop sd other_crop sd

matrix colnames Y =  % sd

matrix list Y
frmttable using Table_1_Whole_Farmer_sample.doc, replace  statmat(Y) ctitle("", "mean", "sd") sdec(2)
restore





preserve
drop if non_farmer==1

keep if country==3|country==6
mat Y = J(100,5,.)
local tt "   no_species pdd9 pdd_livestock pdd_crop other_crop"
foreach x of varlist     other_crop{
replace `x'=`x'*100
}


local g 1
foreach var of varlist `tt'  {
ttest `var', by(country)
mat Y[`g',1] = r(mu_1)
mat Y[`g',2] = r(mu_2)
local g=`g'+1
mat Y[`g',1] = r(sd_1)
mat Y[`g',2] = r(sd_2)
local g=`g'+1

}

matrix rownames Y =  no_species sd pdd9 sd pdd_livestock sd pdd_crop sd other_crop sd

matrix colnames Y =  % sd

matrix list Y
frmttable using Table_1_Ethiopia_Malawi_Farmer_sample.doc, replace  statmat(Y) ctitle("", "Ethiopia", "Malawi") sdec(2)
restore




preserve
drop if non_farmer==1

keep if country==1|country==2
mat Y = J(100,5,.)
local tt "   no_species pdd9 pdd_livestock pdd_crop other_crop"
foreach x of varlist     other_crop{
replace `x'=`x'*100
}


local g 1
foreach var of varlist `tt'  {
ttest `var', by(country)
mat Y[`g',1] = r(mu_1)
mat Y[`g',2] = r(mu_2)
local g=`g'+1
mat Y[`g',1] = r(sd_1)
mat Y[`g',2] = r(sd_2)
local g=`g'+1

}

matrix rownames Y =  no_species sd pdd9 sd pdd_livestock sd pdd_crop sd other_crop sd

matrix colnames Y =  % sd

matrix list Y
frmttable using Table_1_Niger_Nigeria_Farmer_sample.doc, replace  statmat(Y) ctitle("", "Niger", "Nigeria") sdec(2)
restore



preserve
drop if non_farmer==1

keep if country==4|country==5
replace country=0 if country==5     //change the order of country

mat Y = J(100,5,.)
local tt "   no_species pdd9 pdd_livestock pdd_crop other_crop"
foreach x of varlist     other_crop{
replace `x'=`x'*100
}


local g 1
foreach var of varlist `tt'  {
ttest `var', by(country)
mat Y[`g',1] = r(mu_1)
mat Y[`g',2] = r(mu_2)
local g=`g'+1
mat Y[`g',1] = r(sd_1)
mat Y[`g',2] = r(sd_2)
local g=`g'+1

}

matrix rownames Y =  no_species sd pdd9 sd pdd_livestock sd pdd_crop sd other_crop sd

matrix colnames Y =  % sd

matrix list Y
frmttable using Table_1_Tanzania_Uganda_Farmer_sample.doc, replace  statmat(Y) ctitle("", "Tanzania", "Uganda") sdec(2)
restore










tab year, generate(year_)
foreach x of varlist hhsize dependent_share head_age female_head head_read  motobike phone  electricity wagejob enterprise weather_shock  plot_area other_crop hdd9 pdd9 no_species{
egen `x'_mean=mean(`x'), by(HHID_panel)
}

global year_NIGER year_4
global year_NIGERIA year_3 year_5 year_8
global year_ETHIOPIA year_6
global year_UGANDA   year_3 year_4 year_6 year_8 year_10
global year_TANZANIA year_1 year_5 year_7
global year_MALAWI year_3 year_6 
global xlist hhsize dependent_share head_age female_head head_read  motobike phone  electricity wagejob enterprise weather_shock  plot_area other_crop  motobike_mean phone_mean electricity_mean wagejob_mean enterprise_mean  




********************************************************************************
*               Figure 1_Impact of Production Diversity by Countries           *
********************************************************************************
preserve
eststo clear
*whole
xtreg hdd9  pdd9   $xlist  pdd9_mean i.country i.year  , cluster (HHID_panel) 
eststo est1
rename  pdd9       pdd9_1
rename  no_species pdd9
xtreg hdd9  pdd9   $xlist  no_species_mean i.country i.year  , cluster (HHID_panel) 
eststo est2

*Ethiopia
xtreg hdd9  pdd9_1    $xlist  pdd9_mean $year_ETHIOPIA if country==3, cluster (HHID_panel) 
eststo est3
rename  pdd9_1       pdd9_2
rename  pdd9       pdd9_1
xtreg hdd9  pdd9_1    $xlist  no_species_mean $year_ETHIOPIA if country==3, cluster (HHID_panel) 
eststo est4

*Malawi
xtreg hdd9  pdd9_2    $xlist  pdd9_mean $year_MALAWI if country==6, cluster (HHID_panel) 
eststo est5
rename  pdd9_2       pdd9_3
rename  pdd9_1       pdd9_2

xtreg hdd9  pdd9_2    $xlist  no_species_mean $year_MALAWI if country==6, cluster (HHID_panel) 
eststo est6

*Niger
xtreg hdd9  pdd9_3    $xlist  pdd9_mean $year_NIGER if country==1, cluster (HHID_panel) 
eststo est7
rename  pdd9_3      pdd9_4
rename  pdd9_2        pdd9_3
xtreg hdd9  pdd9_3    $xlist  no_species_mean $year_NIGER if country==1, cluster (HHID_panel) 
eststo est8


*Nigeria
xtreg hdd9  pdd9_4    $xlist  pdd9_mean $year_NIGERIA if country==2, cluster (HHID_panel) 
eststo est9
rename  pdd9_4       pdd9_5
rename  pdd9_3       pdd9_4
xtreg hdd9  pdd9_4    $xlist  no_species_mean $year_NIGERIA if country==2, cluster (HHID_panel) 
eststo est10



*Tanzania
xtreg hdd9  pdd9_5    $xlist  pdd9_mean $year_TANZANIA if country==5, cluster (HHID_panel) 
eststo est11
rename  pdd9_5       pdd9_6
rename  pdd9_4       pdd9_5
xtreg hdd9  pdd9_5    $xlist  no_species_mean $year_TANZANIA if country==5, cluster (HHID_panel) 
eststo est12



*Uganda
xtreg hdd9  pdd9_6    $xlist  pdd9_mean $year_UGANDA if country==4, cluster (HHID_panel) 
eststo est13
drop    pdd9_6
rename  pdd9_5       pdd9_6
xtreg hdd9  pdd9_6    $xlist  no_species_mean $year_UGANDA if country==4, cluster (HHID_panel) 
eststo est14


coefplot (est1, label("Farm production diversity (food group count)") msymbol(d) mlcolor(red%80) mfcolor(white)    ) (est2, label("") msymbol(s) mcolor(navy) mfcolor(white)  )    (est3, label("")  msymbol(d) mlcolor(red%80) mfcolor(white) ) (est4, label("") msymbol(s) mcolor(navy) mfcolor(white)  ) ///    
 (est5,label("") msymbol(d) mlcolor(red%80) mfcolor(white)) (est6,  label("") msymbol(s) mcolor(navy) mfcolor(white)  )   (est7, label("") msymbol(d) mlcolor(red%80) mfcolor(white)  )  (est8, label("Farm production diversity (species count)") msymbol(s) mcolor(navy) mfcolor(white)  ) /// 
 (est9, label("") msymbol(d) mlcolor(red%80) mfcolor(white)) (est10, label("") msymbol(s) mcolor(navy) mfcolor(white)  )   (est11, label("") msymbol(d) mlcolor(red%80) mfcolor(white)  )  (est12, label("") msymbol(s) mcolor(navy) mfcolor(white)  ) /// 
 (est13, label("") msymbol(d) mlcolor(red%80) mfcolor(white) ) (est14,label("")  msymbol(s) mcolor(navy) mfcolor(white)  )  /// 
,  byopts(xrescale) keep(pdd9 pdd9_1 pdd9_2 pdd9_3  pdd9_4 pdd9_5 pdd9_6) vertical ciopts(lcolor(grey) color(%50)  recast(rcap))  /// 
legend(  size(small) rows(2) nobox region(color(white) lstyle(white))) ///  
xlabel(1 "All countries" 2 "Ethiopia" 3 "Malawi" 4 "Niger"    5 "Nigeria" 6 "Tanzania" 7 "Uganda" , labsize(small))   /// 
  ytitle("HDDS", size(small)) levels(95)  plotregion(margin(0)) offset(0)
graph save "Figure_1_Impacts_FGPRD_SCPD.gph", replace
restore 







********************************************************************************
*                  Figure 2_Determinants_Pooled_All Factors                    *
********************************************************************************
eststo clear
xtreg hdd9  pdd9   $xlist  pdd9_mean i.country i.year  , cluster (HHID_panel) 
eststo clear
eststo est1


coefplot (est1 , msymbol(d) msize(medlarge) mfcolor(white) mlcolor(red%80) mlwidth(medium)), xline(0) keep(pdd9 hhsize dependent_share head_age female_head head_read  motobike phone  electricity wagejob enterprise weather_shock  plot_area other_crop  ) horizontal byopts(compact cols(1))  ///
ylabel(1 "FPD" 2 "Household size" 3 "Dependent share" 4 "Head age"    5 "Female head" 6  "Head literacy" 7 "Motobike" ///
8 "Phone"  9 "Electrictity"  10 "Wage employment" 11 "Non-farm enterprise"  12 "Weather shock"  13 "Farm area"  14 "Non-food cash crop"  , nogrid labsize(small))  legend( size(small) position(6) rows(2) nobox region(color(white) lstyle(none)))  ///
 yla( 1 (1) 14) ciopts(lcolor(black) recast(rcap 	) color(%60)) plotr(color(white)) levels(95)
graph save "Figure_2_Determinants.gph", replace







********************************************************************************
*               Figure 3a_Impacts by distance to urban center                  *
********************************************************************************
preserve
drop if dist_popcenter==.

drop pdd9_mean  no_species_mean hhsize_mean dependent_share_mean head_age_mean female_head_mean head_read_mean motobike_mean phone_mean electricity_mean wagejob_mean enterprise_mean weather_shock_mean plot_area_mean other_crop_mean
egen pdd9_mean=mean(pdd9), by(HHID_panel)
egen pdd9_mean_vill=mean(pdd9_vill), by(HHID_panel)
egen pdd9_mean_town=mean(pdd9_town), by(HHID_panel)
egen pdd9_mean_dist=mean(pdd9_dist), by(HHID_panel)

foreach x of varlist hhsize dependent_share head_age female_head head_read  motobike phone  electricity wagejob enterprise weather_shock  plot_area other_crop{
egen `x'_mean=mean(`x'), by(HHID_panel)
}



eststo clear
xtreg hdd9  c.pdd9##c.dist_popcenter   $xlist  pdd9_mean  i.country i.year  , cluster (HHID_panel) 
margins , dydx(pdd9)  at(dist_popcenter=(0 (10) 150))  level(95) saving(file1, replace)


xtreg hdd9  c.pdd9##c.dist_popcenter   $xlist  pdd9_mean  year_6 if country==3, cluster (HHID_panel) 
margins , dydx(pdd9)  at(dist_popcenter=(0 (10) 150)) level(95) saving(file2, replace)


xtreg hdd9  c.pdd9##c.dist_popcenter   $xlist  pdd9_mean year_3  if country==6, cluster (HHID_panel) 
margins , dydx(pdd9)  at(dist_popcenter=(0 (10) 150)) level(95) saving(file3, replace)

 
xtreg hdd9  c.pdd9##c.dist_popcenter    $xlist  pdd9_mean  year_4 if country==1, cluster (HHID_panel) 
 margins , dydx(pdd9)  at(dist_popcenter=(0 (10) 150)) level(95) saving(file4, replace)


xtreg hdd9  c.pdd9##c.dist_popcenter    $xlist  pdd9_mean  year_3 year_5 year_8 if country==2, cluster (HHID_panel) 
margins , dydx(pdd9)  at(dist_popcenter=(0 (10) 150)) level(95) saving(file5, replace)


xtreg hdd9  c.pdd9##c.dist_popcenter   $xlist  pdd9_mean year_1 year_3  if country==5, cluster (HHID_panel) 
 margins , dydx(pdd9)  at(dist_popcenter=(0 (10) 150)) level(95) saving(file6, replace)

xtreg hdd9  c.pdd9##c.dist_popcenter   $xlist  pdd9_mean  year_2 year_3  if country==4, cluster (HHID_panel) 
 margins , dydx(pdd9)  at(dist_popcenter=(0 (10) 150)) level(95) saving(file7, replace)

combomarginsplot  file1 file2 file3 file4 file5  file6 file7, labels( "All countries" "Ethiopia" "Malawi"  "Niger" "Nigeria"  "Tanzania"  "Uganda", size(small) )     ci1opt(color(%5))  ci2opt(color(%5)) ci3opt(color(%5)) ci4opt(color(%5)) ci5opt(color(%5)) ci6opt(color(%5)) ci7opt(color(%5)) title("Impact of production diversity on household dietary diversity by proximity to a center area", size(small))   xlabel(, labsize(small))   xtitle("Distance (km)", size(small)) ytitle("HDDS", size(small)) legend(size(small) rows(2) nobox region(lstyle(none)))  recastci(rcap) file1opts(msymbol(c) mlcolor(navy) mfcolor(navy))  file2opts(msymbol(X) mlcolor(orange) mfcolor(white))  file3opts(msymbol(t) mlcolor(lavender) mfcolor(white))  file4opts(msymbol(d) mlcolor(gold) mfcolor(white)) file5opts(msymbol(s) mlcolor(green) mfcolor(white)) file6opts(msymbol(|) mlcolor(red) mfcolor(white)) file7opts(msymbol(c) mlcolor(cranberry) mfcolor(white)) plotopts( lpattern(shortdash) lcolor(%10))
  graph save "Figure_3a_Distance_Pop.gph", replace
restore




********************************************************************************
*                          Figure 3c                                           *         
********************************************************************************
preserve
eststo clear
drop if hdd9_own==.|hdd9_purchase==.
drop pdd9_mean hhsize_mean dependent_share_mean head_age_mean female_head_mean head_read_mean motobike_mean phone_mean electricity_mean wagejob_mean enterprise_mean weather_shock_mean plot_area_mean other_crop_mean
egen pdd9_mean=mean(pdd9), by(HHID_panel)

foreach x of varlist hhsize dependent_share head_age female_head head_read  motobike phone  electricity wagejob enterprise weather_shock  plot_area other_crop{
egen `x'_mean=mean(`x'), by(HHID_panel)
}


*whole
xtreg hdd9_own  pdd9   $xlist  pdd9_mean i.country i.year  , cluster (HHID_panel) 
eststo est1
xtreg hdd9_purchase  pdd9   $xlist  pdd9_mean i.country i.year  , cluster (HHID_panel) 
eststo est2


rename  pdd9       pdd9_1
*Ethiopia
xtreg hdd9_own  pdd9_1    $xlist  pdd9_mean $year_ETHIOPIA if country==3, cluster (HHID_panel) 
eststo est3
xtreg hdd9_purchase  pdd9_1    $xlist  pdd9_mean $year_ETHIOPIA if country==3, cluster (HHID_panel) 
eststo est4


rename  pdd9_1      pdd9_2
*Malawi
xtreg hdd9_own  pdd9_2    $xlist  pdd9_mean $year_MALAWI if country==6, cluster (HHID_panel) 
eststo est5
xtreg hdd9_purchase  pdd9_2    $xlist  pdd9_mean $year_MALAWI if country==6, cluster (HHID_panel) 
eststo est6


rename  pdd9_2         pdd9_3
*Niger
xtreg hdd9_own  pdd9_3    $xlist  pdd9_mean $year_NIGER if country==1, cluster (HHID_panel) 
eststo est7
xtreg hdd9_purchase  pdd9_3    $xlist  pdd9_mean $year_NIGER if country==1, cluster (HHID_panel) 
eststo est8

rename  pdd9_3       pdd9_4
*Nigeria
xtreg hdd9_own  pdd9_4   $xlist  pdd9_mean $year_NIGERIA if country==2, cluster (HHID_panel) 
eststo est9
xtreg hdd9_purchase  pdd9_4    $xlist  pdd9_mean $year_NIGERIA if country==2, cluster (HHID_panel) 
eststo est10


rename  pdd9_4       pdd9_5
*Tanzania
xtreg hdd9_own  pdd9_5    $xlist  pdd9_mean $year_TANZANIA if country==5, cluster (HHID_panel) 
eststo est11
xtreg hdd9_purchase  pdd9_5    $xlist  pdd9_mean $year_TANZANIA if country==5, cluster (HHID_panel) 
eststo est12

rename  pdd9_5       pdd9_6
*Uganda
xtreg hdd9_own  pdd9_6    $xlist  pdd9_mean $year_UGANDA if country==4, cluster (HHID_panel) 
eststo est13
xtreg hdd9_purchase  pdd9_6    $xlist  pdd9_mean $year_UGANDA if country==4, cluster (HHID_panel) 
eststo est14

coefplot (est1, label("HDDS from own production") msymbol(d) mlcolor(red%80) mfcolor(white)    ) (est2, label("") msymbol(s) mlcolor(navy) mfcolor(white)  )    (est3, label("") msymbol(d) mlcolor(red%80) mfcolor(white) ) (est4, label("") msymbol(s) mlcolor(navy) mfcolor(white)) ///    
 (est5,label("") msymbol(d) mlcolor(red%80) mfcolor(white)) (est6,  label("") msymbol(s) mlcolor(navy) mfcolor(white))   (est7, label("") msymbol(d) mlcolor(red%80) mfcolor(white)  )  (est8, label("HDDS from market purchase") msymbol(s) mlcolor(navy) mfcolor(white)) /// 
 (est9, label("") msymbol(d) mlcolor(red%80) mfcolor(white)) (est10, label("") msymbol(s) mlcolor(navy) mfcolor(white))   (est11, label("") msymbol(d) mlcolor(red%80) mfcolor(white)  )  (est12, label("") msymbol(s) mlcolor(navy) mfcolor(white)) /// 
 (est13, label("") msymbol(d) mlcolor(red%80) mfcolor(white)) (est14, label("") msymbol(s) mlcolor(navy) mfcolor(white))  /// 
,  keep(pdd9 pdd9_1 pdd9_2 pdd9_3  pdd9_4 pdd9_5 pdd9_6) vertical ciopts(lcolor(grey%50)  recast(rcap))  ///
legend(  size(small) rows(2) nobox region(color(ltbluishgray) lstyle(none))) ///  
xmtick(0.6 1.6 2.8 4 5.1 6.2 7.4) xlabel( 0.6 "All countries" 1.6 "Ethiopia" 2.8 "Malawi" 4 "Niger"    5.1 "Nigeria" 6.2 "Tanzania" 7.4 "Uganda", labsize(small)) xscale(r(0(0.5)3.5)) ylabel(-0.1 0 0.1 0.2 0.3) ymtick(-0.1 0 0.1 0.2 0.3)   ///
  ytitle("HDDS", size(small)) levels(95) 

graph save "Figure_3c_Sources.gph", replace
restore 







********************************************************************************
*          Figure 4_Impact of Production Diversity by regional levels          *
********************************************************************************
preserve
drop if sum_vill<=1|sum_vill==.

drop pdd9_mean no_species_mean hhsize_mean dependent_share_mean head_age_mean female_head_mean head_read_mean motobike_mean phone_mean electricity_mean wagejob_mean enterprise_mean weather_shock_mean plot_area_mean other_crop_mean
egen pdd9_mean=mean(pdd9), by(HHID_panel)
egen pdd9_mean_vill=mean(pdd9_vill), by(HHID_panel)
egen pdd9_mean_town=mean(pdd9_town), by(HHID_panel)
egen pdd9_mean_dist=mean(pdd9_dist), by(HHID_panel)

foreach x of varlist hhsize dependent_share head_age female_head head_read  motobike phone  electricity wagejob enterprise weather_shock  plot_area other_crop{
egen `x'_mean=mean(`x'), by(HHID_panel)
}


eststo clear
*Whole Sample
gen pdd9_whole=pdd9
xtreg hdd9  pdd9_whole   $xlist  pdd9_mean i.country i.year  , cluster (HHID_panel) 
eststo est1

drop pdd9_whole
gen pdd9_whole= pdd9_vill
xtreg hdd9  pdd9_whole sum_vill  $xlist  pdd9_mean_vill i.country i.year  , cluster (HHID_panel) 
eststo est2


drop pdd9_whole
gen pdd9_whole= pdd9_town
xtreg hdd9  pdd9_whole sum_town  $xlist  pdd9_mean_town i.country i.year, cluster (HHID_panel) 
eststo est3


drop pdd9_whole
gen pdd9_whole= pdd9_dist
xtreg hdd9  pdd9_whole sum_dist  $xlist  pdd9_mean_dist i.country i.year, cluster (HHID_panel) 
eststo est4






*Ethiopia
gen pdd9_3=pdd9
xtreg hdd9  pdd9_3   $xlist  pdd9_mean $year_ETHIOPIA if country==3, cluster (HHID_panel) 
eststo est11

drop pdd9_3
gen pdd9_3= pdd9_vill
xtreg hdd9  pdd9_3 sum_vill  $xlist  pdd9_mean_vill $year_ETHIOPIA if country==3, cluster (HHID_panel) 
eststo est21


drop pdd9_3
gen pdd9_3= pdd9_town
xtreg hdd9  pdd9_3 sum_town  $xlist  pdd9_mean_town $year_ETHIOPIA if country==3, cluster (HHID_panel) 
eststo est31


drop pdd9_3
gen pdd9_3= pdd9_dist
xtreg hdd9  pdd9_3 sum_dist  $xlist  pdd9_mean_dist $year_ETHIOPIA if country==3, cluster (HHID_panel) 
eststo est41




*Malawi
gen pdd9_6=pdd9
xtreg hdd9  pdd9_6   $xlist  pdd9_mean $year_MALAWI if country==6, cluster (HHID_panel) 
eststo est12

drop pdd9_6
gen pdd9_6= pdd9_vill
xtreg hdd9  pdd9_6 sum_vill  $xlist  pdd9_mean_vill $year_MALAWI if country==6, cluster (HHID_panel) 
eststo est22


drop pdd9_6
gen pdd9_6= pdd9_town
xtreg hdd9  pdd9_6 sum_town  $xlist  pdd9_mean_town $year_MALAWI if country==6, cluster (HHID_panel) 
eststo est32


drop pdd9_6
gen pdd9_6= pdd9_dist
xtreg hdd9  pdd9_6 sum_dist  $xlist  pdd9_mean_dist $year_MALAWI if country==6, cluster (HHID_panel) 
eststo est42





*Niger
gen pdd9_1=pdd9
xtreg hdd9  pdd9_1   $xlist  pdd9_mean $year_NIGER if country==1, cluster (HHID_panel) 
eststo est13

drop pdd9_1
gen pdd9_1= pdd9_vill
xtreg hdd9  pdd9_1 sum_vill  $xlist  pdd9_mean_vill $year_NIGER if country==1, cluster (HHID_panel) 
eststo est23


drop pdd9_1
gen pdd9_1= pdd9_town
xtreg hdd9  pdd9_1 sum_town  $xlist  pdd9_mean_town $year_NIGER if country==1, cluster (HHID_panel) 
eststo est33


drop pdd9_1
gen pdd9_1= pdd9_dist
xtreg hdd9  pdd9_1 sum_dist  $xlist  pdd9_mean_dist $year_NIGER if country==1, cluster (HHID_panel) 
eststo est43



*Nigeria
gen pdd9_2=pdd9
xtreg hdd9  pdd9_2   $xlist  pdd9_mean $year_NIGERIA if country==2, cluster (HHID_panel) 
eststo est14

drop pdd9_2
gen pdd9_2= pdd9_vill
xtreg hdd9  pdd9_2 sum_vill  $xlist  pdd9_mean_vill $year_NIGERIA if country==2, cluster (HHID_panel) 
eststo est24


drop pdd9_2
gen pdd9_2= pdd9_town
xtreg hdd9  pdd9_2 sum_town  $xlist  pdd9_mean_town $year_NIGERIA if country==2, cluster (HHID_panel) 
eststo est34


drop pdd9_2
gen pdd9_2= pdd9_dist
xtreg hdd9  pdd9_2 sum_dist  $xlist  pdd9_mean_dist $year_NIGERIA if country==2, cluster (HHID_panel) 
eststo est44


*Tanzania
gen pdd9_5=pdd9
xtreg hdd9  pdd9_5   $xlist  pdd9_mean $year_TANZANIA if country==5, cluster (HHID_panel) 
eststo est15

drop pdd9_5
gen pdd9_5= pdd9_vill
xtreg hdd9  pdd9_5 sum_vill  $xlist  pdd9_mean_vill $year_TANZANIA if country==5, cluster (HHID_panel) 
eststo est25


drop pdd9_5
gen pdd9_5= pdd9_town
xtreg hdd9  pdd9_5 sum_town  $xlist  pdd9_mean_town $year_TANZANIA if country==5, cluster (HHID_panel) 
eststo est35


drop pdd9_5
gen pdd9_5= pdd9_dist
xtreg hdd9  pdd9_5 sum_dist  $xlist  pdd9_mean_dist $year_TANZANIA if country==5, cluster (HHID_panel) 
eststo est45



*Uganda
gen pdd9_4=pdd9
xtreg hdd9  pdd9_4   $xlist  pdd9_mean $year_UGANDA if country==4, cluster (HHID_panel) 
eststo est16

drop pdd9_4
gen pdd9_4= pdd9_vill
xtreg hdd9  pdd9_4 sum_vill  $xlist  pdd9_mean_vill $year_UGANDA if country==4, cluster (HHID_panel) 
eststo est26


drop pdd9_4
gen pdd9_4= pdd9_town
xtreg hdd9  pdd9_4 sum_town  $xlist  pdd9_mean_town $year_UGANDA if country==4, cluster (HHID_panel) 
eststo est36


drop pdd9_4
gen pdd9_4= pdd9_dist
xtreg hdd9  pdd9_4 sum_dist  $xlist  pdd9_mean_dist $year_UGANDA if country==4, cluster (HHID_panel) 
eststo est46




coefplot (est1, label("Farm-level production diversity") msymbol(d) mfcolor(white) mlcolor(red)   ) (est2, label("") msymbol(s) mlcolor(navy) mfcolor(white))  (est3, label("") msymbol(o) mlcolor(orange) mfcolor(white))  (est4, label("") msymbol(t) mlcolor(green) mfcolor(white)) ///
(est11, label("")  msymbol(d) mfcolor(white) mlcolor(red)) (est21, label("Village-level production diversity")  msymbol(s) mcolor(navy) mfcolor(white))  (est31,  label("") msymbol(o) mlcolor(orange) mfcolor(white))  (est41,  label("")  msymbol(t) mlcolor(green) mfcolor(white)) ///  
(est12, label("")  msymbol(d) mfcolor(white) mlcolor(red)) (est22, label("")  msymbol(s) mcolor(navy) mfcolor(white))  (est32,  label("Town-level production diversity") msymbol(o) mlcolor(orange) mfcolor(white))  (est42,  label("") msymbol(t) mlcolor(green) mfcolor(white)) ///  
(est13, label("")  msymbol(d) mfcolor(white) mlcolor(red)) (est23, label("")  msymbol(s) mcolor(navy) mfcolor(white))  (est33,  label("") msymbol(o) mlcolor(orange) mfcolor(white))  (est43,  label("District-level production diversity") msymbol(t) mlcolor(green) mfcolor(white)) ///  
(est14, label("")  msymbol(d) mfcolor(white) mlcolor(red)) (est24, label("")  msymbol(s) mcolor(navy) mfcolor(white))  (est34,  label("") msymbol(o) mlcolor(orange) mfcolor(white))  (est44,  label("") msymbol(t) mlcolor(green) mfcolor(white)) ///  
(est15, label("")  msymbol(d) mfcolor(white) mlcolor(red)) (est25, label("")  msymbol(s) mcolor(navy) mfcolor(white))  (est35,  label("") msymbol(o) mlcolor(orange) mfcolor(white))  (est45,  label("") msymbol(t) mlcolor(green) mfcolor(white)) ///  
(est16, label("")  msymbol(d) mfcolor(white) mlcolor(red)) (est26, label("")  msymbol(s) mcolor(navy) mfcolor(white))  (est36,  label("") msymbol(o) mlcolor(orange) mfcolor(white))  (est46,  label("") msymbol(t) mlcolor(green) mfcolor(white)) ///  
,  keep(pdd9_whole pdd9_1 pdd9_2 pdd9_3  pdd9_4 pdd9_5 pdd9_6) vertical ciopts(lcolor(black%10) recast(rcap))  ///
legend( size(small) rows(3) nobox region(color(ltbluishgray) lstyle(none))) ///  
  ytitle("HDDS", size(small)) levels(95) xmtick(0.6 1.6 2.8 4 5.1 6.2 7.4) xlabel( 0.6 "All countries" 1.6 "Ethiopia" 2.8 "Malawi" 4 "Niger"    5.1 "Nigeria" 6.2 "Tanzania" 7.4 "Uganda", labsize(small)) xscale(r(0(0.5)3.5)) ylabel(-0.1 0 0.1 0.2 0.3) ymtick(-0.1 0 0.1 0.2 0.3)
  graph save "Figure_4.gph", replace

restore








********************************************************************************
*                            Figure 3b                                         *
********************************************************************************
drop if own_value_share==.
gen subsistence=1 if own_value_share>0.50&own_value_share<.
replace subsistence=0 if own_value_share<=0.50

preserve
collapse (mean) hdd9 pdd9, by(subsistence) 
save whole_sample_graph_subsistence, replace
restore

collapse (mean) hdd9 pdd9, by(country subsistence) 
append using whole_sample_graph_subsistence


gen country_new=1 if country==3
replace country_new=2 if country==6
replace country_new=3 if country==1
replace country_new=4 if country==2
replace country_new=5 if country==5
replace country_new=6 if country==4
drop country
gen country=country_new

replace country=0 if country==.
gen country_1=country-0.3
gen country_2=country

twoway bar hdd9 country_1 if subsistence==0, barw(0.3)  color(navy%80) || scatter  pdd9 country_1 if subsistence==0, msymbol(S) mfcolor(white) mlcolor(red)||bar hdd9 country_2 if subsistence==1, barw(0.3)  color(red%50)   || scatter  pdd9 country_2 if subsistence==1, msymbol(D) mfcolor(white) mlcolor(navy) ///
 xlabel(-0.15 "All countries" 0.85 "Ethiopia" 1.85 "Malawi" 2.85 "Niger" 3.85 "Nigeria" 4.85 "Tanzania" 5.85 "Uganda", labsize(small)) xmtick(-0.15 0.85 1.85 2.85 3.85 4.85 5.85) ytitle("Diversity Score", size(small)) plotregion(margin(0)) ymtick(0(1)9) ylabel(0 1 2 3 4 5 6 )   ///
  leg(on order(1 2 3 4)  label(1 "HDDS among non-subsistence households")label(2 "FPD among non-subsistence households" ) label(3 "HDDS among subsistence households" ) label(4 "FPD among subsistence households" ) col(2) ring(5) pos(6) size(vsmall) )
  graph save "Figure_3b.gph", replace
log close




